package com.wxl.xueyuan;

import android.Manifest;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.wxl.xueyuan.activity.BaseActivity;
import com.wxl.xueyuan.activity.HomeActivity;
import com.wxl.xueyuan.activity.LoginActivity;
import com.wxl.xueyuan.util.ActivityCollectorUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Button btnLogin;
    private static final int PERMISSION_REQUEST = 1001;
    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
    List<String> permissionsList = new ArrayList<>();
    /**
     * 请求权限
     */
    private void initPermissions() {
        permissionsList.clear();

        //判断哪些权限未授予
        for(String permission : permissions){
            if(ActivityCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED){
                permissionsList.add(permission);
            }
        }

        //请求权限
        if(!permissionsList.isEmpty()){
            String[] permissions = permissionsList.toArray(new String[permissionsList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST);
        }
    }

    /**
     * 权限回调,
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode){
            case PERMISSION_REQUEST:
                break;
            default:
                break;
        }
    }
    @Override
    protected int initLayout() {
        initPermissions();
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initPermissions();
        btnLogin = findViewById(R.id.btn_login);
        ActivityCollectorUtil.addActivity(MainActivity.this);
    }

    @Override
    protected void initData() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(LoginActivity.class);
            }
        });
    }
}